package response

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

type Response struct {
	Code int         `json:"code"`
	Data interface{} `json:"data"`
	Msg  string      `json:"msg"`
}

func Result(c *gin.Context, statusCode, code int, data interface{}, msg string) {
	c.JSON(statusCode, Response{
		code,
		data,
		msg,
	})
}

// BadRequest 请求失败
func BadRequest(c *gin.Context, code int, data any, msg string) {
	Result(c, http.StatusBadRequest, code, data, msg)
}

func BadRequestWithCode(c *gin.Context, code int) {
	BadRequest(c, code, nil, "操作失败")
}

func BadRequestWithData(c *gin.Context, data any) {
	BadRequest(c, http.StatusBadRequest, data, "操作失败")
}

func BadRequestWithMsg(c *gin.Context, msg string) {
	BadRequest(c, http.StatusBadRequest, nil, msg)
}

func BadRequestWithout(c *gin.Context) {
	BadRequest(c, http.StatusBadRequest, nil, "操作失败")
}

// Forbidden 权限不通过
func Forbidden(c *gin.Context, code int, data any, msg string) {
	Result(c, http.StatusForbidden, code, data, msg)
}

func ForbiddenWithCode(c *gin.Context, code int) {
	Forbidden(c, code, nil, "权限不足")
}

func ForbiddenWithData(c *gin.Context, data any) {
	Forbidden(c, http.StatusForbidden, data, "权限不足")
}

func ForbiddenWithMsg(c *gin.Context, msg string) {
	Forbidden(c, http.StatusForbidden, nil, msg)
}

func ForbiddenWithout(c *gin.Context) {
	Forbidden(c, http.StatusForbidden, nil, "权限不足")
}

// Unauthorized 未授权
func Unauthorized(c *gin.Context, code int, data any, msg string) {
	Result(c, http.StatusUnauthorized, code, data, msg)
}

func UnauthorizedWithCode(c *gin.Context, code int) {
	Unauthorized(c, code, nil, "未授权")
}

func UnauthorizedWithData(c *gin.Context, data any) {
	Unauthorized(c, http.StatusUnauthorized, data, "未授权")
}

func UnauthorizedWithMsg(c *gin.Context, msg string) {
	Unauthorized(c, http.StatusUnauthorized, nil, msg)
}

func UnauthorizedWithout(c *gin.Context) {
	Unauthorized(c, http.StatusUnauthorized, nil, "未授权")
}

// InternalServerError 服务器异常
func InternalServerError(c *gin.Context, code int, data any, msg string) {
	Result(c, http.StatusInternalServerError, code, data, msg)
}

func InternalServerErrorWithCode(c *gin.Context, code int) {
	InternalServerError(c, code, nil, "服务器异常")
}

func InternalServerErrorWithData(c *gin.Context, data any) {
	InternalServerError(c, http.StatusInternalServerError, data, "服务器异常")
}

func InternalServerErrorWithMsg(c *gin.Context, msg string) {
	InternalServerError(c, http.StatusInternalServerError, nil, msg)
}

func InternalServerErrorWithout(c *gin.Context) {
	InternalServerError(c, http.StatusInternalServerError, nil, "服务器异常")
}

// Success 成功
func Success(c *gin.Context, code int, data any, msg string) {
	Result(c, http.StatusOK, code, data, msg)
}

func SuccessWithCode(c *gin.Context, code int) {
	Success(c, code, nil, "操作成功")
}

func SuccessWithData(c *gin.Context, data any) {
	Success(c, http.StatusOK, data, "操作成功")
}

func SuccessWithMsg(c *gin.Context, msg string) {
	Success(c, http.StatusOK, nil, msg)
}

func SuccessWithout(c *gin.Context) {
	Success(c, http.StatusOK, nil, "操作成功")
}
